package net.dopan.pigframe.demo.service.impl;

import net.dopan.pigframe.config.utils.ObjToMap;
import net.dopan.pigframe.config.utils.Sort;
import net.dopan.pigframe.core.base.service.impl.BaseServiceImpl;
import net.dopan.pigframe.demo.mapper.HomePageMapper;
import net.dopan.pigframe.demo.model.entity.UserDemo;
import net.dopan.pigframe.demo.model.entity.vo.EventUserNoteVo1;
import net.dopan.pigframe.demo.service.HomepageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class HomepageServiceImpl extends BaseServiceImpl<HomePageMapper, UserDemo> implements HomepageService {

    @Autowired
    private HomePageMapper homePageMapper;

    @Override
    public Boolean checkRelationship(String userId, String friendId) {
        System.err.println(homePageMapper.checkRelationship(userId, friendId) + "11111111111111111111");
        System.err.println(!homePageMapper.checkRelationship(userId, friendId).isEmpty());
        if (homePageMapper.checkRelationship(userId, friendId).size() == 2) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean checkConcern(String userId, String friendId) {
        System.err.println(homePageMapper.checkConcern(userId, friendId).isEmpty());
        if (homePageMapper.checkConcern(userId, friendId).isEmpty()) {
            return false;
        }
        return true;
    }

    @Override
    public Integer addConcern(String userId, String friendId) {
        //String concernUsername = homePageMapper.findUsernameById(concernUserId);
//        String fansname = homePageMapper.findUsernameById(userId);
//        /** 添加关注 */
//        homePageMapper.concernUser(userId, concernUserId, concernUsername);
//        /**修改用户关注数量 */
//        homePageMapper.updateUserConcernNum(homePageMapper.concernCount(userId), userId);
//        /** 添加粉丝 */
//        homePageMapper.addfans(concernUserId, userId, fansname);
//        /** 修改用户粉丝数量 */
//        homePageMapper.updatefansNum(homePageMapper.fansCount(concernUserId), concernUserId);
        return null;
    }

    @Override
    public void concernUser(String userId, String concernUserId) {
        String concernUsername = homePageMapper.findUsernameById(concernUserId);
        String fansname = homePageMapper.findUsernameById(userId);
        /** 添加关注 */
        homePageMapper.concernUser(userId, concernUserId, concernUsername);
        /**修改用户关注数量 */
        homePageMapper.updateUserConcernNum(homePageMapper.concernCount(userId), userId);
        /** 添加粉丝 */
        homePageMapper.addfans(concernUserId, userId, fansname);
        /** 修改用户粉丝数量 */
        homePageMapper.updatefansNum(homePageMapper.fansCount(concernUserId), concernUserId);
    }

    @Override
    public Boolean checkFans(String userId, String friendId) {
        if (homePageMapper.checkFans(userId, friendId) != null) {
            return true;
        }
        return false;
    }

    @Override
    public Integer addFans(String userId, String friendId) {
        String username = homePageMapper.findUsername(friendId);
        return homePageMapper.addFans(userId, friendId, username);
    }

    @Override
    public Map<String, List> getFriends(String userId) {
        List<Map<String, String>> list = homePageMapper.getFriends(userId);
        return Sort.sort(list);
    }

    @Override
    public List<Map<String, String>> getUserEvents(String userId, Integer offset, Integer limit) {
        /** 获取好友列表 */
        List<String> userIds = new ArrayList<>();
        List<Map<String, String>> list = homePageMapper.getFriends(userId);
        for (Map map:list
        ) {
            userIds.add(String.valueOf(map.get("user_id")));
        }
        List<Map<String, String>> listAll = homePageMapper.getUserEvent(userId,userIds,offset,limit);
        for (int i = 0; i < listAll.size(); i++) {
            /** 发布活动 */
            Map<String, String> map2 = listAll.get(i);
            String event_type_id = map2.get("event_type_id");
            int id = Integer.parseInt(event_type_id);
            String msgId = map2.get("event_msg_id");
            if (id == 1) {
                Map<String, String> mapList = new HashMap<>();
                /** 查找活动 */
                List<Map<String, String>> list1 = homePageMapper.getEventActivity(msgId,userId);
                for (Map map1 : list1
                ) {
                    mapList.putAll(map1);
                }
                listAll.get(i).putAll(mapList);
            }
            /** 发布笔记 */
            else if (id == 2) {
                Map<String, String> mapList = new HashMap<>();
                /** 查找笔记 */
                List<EventUserNoteVo1> EventUserNoteVo1List = homePageMapper.getEventUserNoteMap(msgId,userId);
                for (EventUserNoteVo1 EventUserNoteVo1 : EventUserNoteVo1List
                ) {
                    Map map = ObjToMap.objectToMap(EventUserNoteVo1);
                    mapList.putAll(map);
                }
                listAll.get(i).putAll(mapList);
            }
            /** 转发活动 */
            else if (id == 3) {
                Map<String, String> mapList = new HashMap<>();
                List<Map<String, String>> list1 = homePageMapper.getEventActivity(msgId,userId);
                for (Map map1 : list1
                ) {
                    mapList.putAll(map1);
                }
                listAll.get(i).putAll(mapList);

            }
            /** 转发笔记 */
            else if (id == 4) {
                Map<String, String> mapList = new HashMap<>();
                /** 查找笔记 */
                List<EventUserNoteVo1> EventUserNoteVo1List = homePageMapper.getEventUserNoteMap(msgId,userId);
                for (EventUserNoteVo1 EventUserNoteVo1 : EventUserNoteVo1List
                ) {
                    Map map = ObjToMap.objectToMap(EventUserNoteVo1);
                    mapList.putAll(map);
                }
                listAll.get(i).putAll(mapList);
            }
        }
        return listAll;
    }
}
